package com.augurit.gzcgwsp.sendNotice.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import com.augurit.gzcgwsp.sendNotice.dao.YwPublicOpinionsDao;
import com.augurit.gzcgwsp.sendNotice.entity.YwPublicOpinions;
import com.augurit.gzcgwsp.sendNotice.service.IYwPublicOpinionsService;
import com.augurit.common.syslog.tool.SysTraceLogWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Transactional
public class YwPublicOpinionsServiceImpl implements IYwPublicOpinionsService {
	Logger logger = (Logger) LoggerFactory.getLogger(CallWebServiceImpl.class);
	@Autowired
	private YwPublicOpinionsDao ywPublicOpinionsDao;
	
	/**
	 * 保存
	 * @param ywPublicOpinions 
	 * @return true或者false
	 */
	public boolean saveOpinions(YwPublicOpinions ywPublicOpinions){
		try {
			ywPublicOpinionsDao.getSession().saveOrUpdate(ywPublicOpinions);
			ywPublicOpinionsDao.getSession().flush();
			return true;
		} catch (Exception e) {
			logger.error("(接口)保存公示意见接口异常"+SysTraceLogWriter.MESSAGE_SEPARATOR + e.getMessage(), e);
		}
		return false;
	}
	
	/**
	 * 通过受理号获取
	 * @param acceptNo 受理号
	 * @return List<YwPublicOpinions>
	 */
	public Page<YwPublicOpinions> getByAcceptNo(Page<YwPublicOpinions> publicOpinionsPage,String acceptNo) {
    	String sql = "from YwPublicOpinions t where t.calls='one' and t.acceptNo='" + acceptNo + "' order by t.submitTime desc";
        List<YwPublicOpinions> entityList = ywPublicOpinionsDao.find(sql);
        //return entityList;
        
        // 查询语句及参数
        StringBuffer hql = new StringBuffer();
        hql.append(sql);
        List values = new ArrayList();        
        // 执行分页查询操作
        ywPublicOpinionsDao.findPage(publicOpinionsPage, hql.toString(), values);
        
        return publicOpinionsPage;
    }
	
	/**
	 * 获取总意见数量
	 * @param acceptNo
	 * @return
	 */
	public long getTotalOpinionsByAcceptNo(String acceptNo){
		String sql = "select count(*) from Yw_Public_Opinions t where t.calls='one' and t.accept_No='" + acceptNo + "'";
//        List entityList = ywPublicOpinionsDao.getSession().createSQLQuery(sql).list();
		BigDecimal obj = (BigDecimal)ywPublicOpinionsDao.getSession().createSQLQuery(sql).uniqueResult();
		return obj.longValue();
	}
	
	/**
	 * 获取反对的意见数量
	 * @param acceptNo
	 * @return
	 */
	public long getFdOpinionsTotalByAcceptNo(String acceptNo){
		String sql = "select count(*) from Yw_Public_Opinions t where t.calls='one' and t.accept_No='" + acceptNo + "' and t.if_Agreed='不满意'";
//        List entityList = ywPublicOpinionsDao.getSession().createSQLQuery(sql).list();
        BigDecimal obj = (BigDecimal)ywPublicOpinionsDao.getSession().createSQLQuery(sql).uniqueResult(); 
        return obj.longValue();
	}
	
	/**
	 * 通过受理号获取反对意见
	 * @param acceptNo 受理号
	 * @return List<YwPublicOpinions>
	 */
	public Page<YwPublicOpinions> getFdOpinionsByAcceptNo(Page<YwPublicOpinions> publicOpinionsPage,String acceptNo) { 
    	String sql = "from YwPublicOpinions t where t.calls='one' and t.acceptNo='" + acceptNo + "' and t.ifAgreed='不满意' order by t.submitTime desc";
        List<YwPublicOpinions> entityList = ywPublicOpinionsDao.find(sql);
        //return entityList;
        
        // 查询语句及参数
        StringBuffer hql = new StringBuffer();
        hql.append(sql);
        List values = new ArrayList();        
        // 执行分页查询操作
        ywPublicOpinionsDao.findPage(publicOpinionsPage, hql.toString(), values);
        
        return publicOpinionsPage;
    }

}
